package com.hainiu.cat.interview.juc;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * create by biji.zhao on 2021/7/7
 */
public class TestLock {

    private ArrayList<Integer> arrayList = new ArrayList<>();

    private Lock lock = new ReentrantLock();

    public static void main(String[] args)  {
//        testBaseLock();

//        testLockInterruptibly();

        testSyncWrite();

//        logSub();
    }

    private static void testBaseLock() {
        final TestLock test = new TestLock();

        for (int i = 0; i < 100; i++) {
            new Thread(() -> test.insert(Thread.currentThread()), i + "").start();
        }
    }

    public void insert(Thread thread) {
        if (lock.tryLock()) {
            try {
                System.out.println(thread.getName()+"得到了锁");
                for(int i= 0; i < 5;i ++) {
                    arrayList.add(i);
                }
            } catch (Exception e) {
                // TODO: handle exception
            }finally {
                System.out.println(thread.getName()+"释放了锁");
                lock.unlock();
            }
        }else {
            System.out.println(thread.getName()+"释放锁失败");
        }
    }

    private static void testLockInterruptibly() {
        TestLock test = new TestLock();
        Thread threadA = new Thread(() -> test.lockInterruptibly(Thread.currentThread()), "A");
        Thread threadB = new Thread(() -> test.lockInterruptibly(Thread.currentThread()), "B");
        threadA.start();
        threadB.start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        threadB.interrupt();
    }

    public void lockInterruptibly(Thread thread){
        try {
            // 注意，如果需要正确中断等待锁的线程，必须将获取锁放在外面
            lock.lockInterruptibly();
        } catch (InterruptedException e) {
            return;
        }
        try {
            System.out.println(thread.getName()+"得到了锁");
            TimeUnit.SECONDS.sleep(3);
        }catch (Exception e) {

        } finally {
            lock.unlock();
            System.out.println(thread.getName()+"释放了锁");
        }
    }

    private ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();


    private static void testSyncWrite() {
        final TestLock test = new TestLock();

        new Thread(() -> test.get(Thread.currentThread()), " A").start();

        new Thread(() -> test.get(Thread.currentThread()), " B").start();
    }

    public void get(Thread thread) {
        rwl.readLock().lock();
        try {
            long start = System.currentTimeMillis();
            while(System.currentTimeMillis() - start <= 1) {
                System.out.println(thread.getName()+"正在进行读操作");
            }
            System.out.println(thread.getName()+"读操作完毕");
        }catch (Exception e) {

        }finally {
            rwl.readLock().unlock();
        }
    }

    public static void logSub() {
        long start = System.currentTimeMillis();

        long a;
        while((a = System.currentTimeMillis() - start) <= 1) {
            System.out.println("=======================" + a);
        }
    }

}
